package sys_config

import (
	"context"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"zuhao-seucre/internal/consts"
	"zuhao-seucre/internal/dao"
	"zuhao-seucre/internal/model/bo"
	"zuhao-seucre/internal/model/entity"
	"zuhao-seucre/internal/service"
)

type sSysConfig struct {
}

func New() *sSysConfig {
	return &sSysConfig{}
}

func init() {
	service.RegisterSysConfig(New())
}

// 获取账号登录安全相关配置
func (s *sSysConfig) GetAccountLoginSecurity(ctx context.Context) (*bo.AccountLoginSecurity, error) {
	var (
		config entity.TSysConfig
	)
	err := dao.TSysConfig.Ctx(ctx).Where(dao.TSysConfig.Columns().Key, consts.ConfigKeyAccountLoginSecurity).Scan(&config)
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeNotFound)
	}

	als := new(bo.AccountLoginSecurity)
	// 转化成json
	json := gjson.New(config.Value)
	als.UseCaptcha = json.Get("useCaptcha").Bool()
	als.UseOtp = json.Get("useOtp").Bool()
	als.LoginFailCount = json.Get("loginFailCount").Int()
	als.LoginFailMin = json.Get("loginFailMin").Int()
	als.OtpIssuer = json.Get("otpIssuer").String()
	return als, nil
}
